package servlet;

import com.alibaba.fastjson.JSON;
import entity.Order_Cart;

import entity.Product_Info;
import entity.Product_Pic_Info;
import entity.User;
import service.OrderCartService;
import service.ProductInfoService;
import service.ProductPicInfoService;
import service.UserService;
import service.impl.OrderCartServiceImpl;
import service.impl.ProductInfoServiceImpl;
import service.impl.ProductPicInfoServiceImpl;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DoOrderCarServlet extends HttpServlet {
	private OrderCartService ocs = new OrderCartServiceImpl();
	private UserService us = new UserServiceImpl();
	private ProductInfoService pis = new ProductInfoServiceImpl();
	private ProductPicInfoService ppis = new ProductPicInfoServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		// 获取用户名
		request.setAttribute("username", user.getUsername());
		// 通过用户名查看对应的用户id(先通过姓名查出对应的对象)
		User user1 = us.queryUserByName(user.getUsername());
		// 通过userID查找对应的购物车信息
		List<Order_Cart> allOrder = ocs.queryAllOrder_CartListByUserId(user1.getUserId());
		// 创建集合用于存储所有的产品信息
		List<Product_Info> allProductInfo = new ArrayList<Product_Info>();
		// 创建集合用于存储所有的产品图片信息
		List<Product_Pic_Info> allProductPicInfo = new ArrayList<Product_Pic_Info>();
		// 根据所有的订单信息查找到对应的产品信息
		if(allOrder!=null){
			for (Order_Cart order_Cart : allOrder) {
				Product_Info product_Info = pis.queryProduct_InfoById(order_Cart.getProduct_id());
				Product_Pic_Info product_Pic_Info = ppis.getProduct_Pic_InfoById(order_Cart.getProduct_id());
				if (product_Info != null && product_Pic_Info != null) {
					allProductInfo.add(product_Info);
					allProductPicInfo.add(product_Pic_Info);
					// 将当前的图片信息加入到产品信息中
					product_Info.setProduct_Pic_Info(product_Pic_Info);
					// 将信息存入当前到购物车订单中
					order_Cart.setProduct_Info(product_Info);
				}
			}
		}
		
		response.getWriter().print(JSON.toJSONString(allOrder));

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

}
